package org.abettor.pushbox.download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.model.SelfMessage;
import org.abettor.pushbox.net.NetTools;
import org.abettor.pushbox.util.FileUtil;

import android.app.Activity;
import android.util.Log;

/**
 * 下载地图的操作
 * 
 * @author jzh
 */
public class DownloadMap {
	private NetTools netTools = new NetTools();
	public static int value = 0;
	private Activity context;

	public DownloadMap(Activity context) {
		this.context = context;
	}

	/**
	 * 读取求解地图的列表
	 * 
	 * @param nickName
	 * @param beginTime
	 * @param pageSize
	 * @return
	 */
	public String downloadQuesstionList(String nickName, Date beginTime,
			int pageSize) {
		// return downloadMapListStr(null,null,pageSize);
		return null;
	}

	/**
	 * 读取网络的信息
	 * 
	 * @param nickname
	 * @param begintime
	 *            基准时间
	 * @param pagesize
	 * @return
	 */
	public String downloadMapListStr(String nickName, Date beginTime,
			String myNickName, int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (nickName != null) {
			map.put("nickName", nickName);
		}
		if (beginTime != null) {
			map.put("beginTime", sdf.format(beginTime));
		}
		if (myNickName != null) {
			map.put("myNickName", myNickName);
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listMapFileUrl, map);
		Log.d(this.getClass().getName(), ret == null ? "null" : ret);
		return ret;
	}

	/**
	 * 下载答案的列表
	 * 
	 * @param mapId
	 * @param pageSize
	 * @return
	 */
	public String downloadAnswerListStr(int mapId, Date beginTime, int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("mapId", String.valueOf(mapId));
		if (beginTime != null) {
			map.put("seperateTime", sdf.format(beginTime));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listAnswerUrl, map);
		return ret;
	}
	/**
	 * 解决该地图列表
	 * @param mapId
	 * @param beginTime
	 * @param pageSize
	 * @return
	 */
	public String downResolvedListStr(int mapId,Date beginTime,int pageSize){
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("mapId", String.valueOf(mapId));
		if (beginTime != null) {
			map.put("seperateTime", sdf.format(beginTime));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listResolvedUserUrl, map);
		return ret;
	}
	/**
	 * 加载早期评论列表的列表
	 * @param mapId
	 * @param beginTime
	 * @param pageSize
	 * @return
	 */
	public String downloadEarlyMessageBoardListStr(int mapId,Date beginTime,int pageSize){
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("mapId", String.valueOf(mapId));
		if (beginTime != null) {
			map.put("beginTime", sdf.format(beginTime));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listCommentEarlyUrl, map);
		return ret;
	}
	/**
	 * 加载最新一期评论的列表
	 * @param mapId
	 * @param endTime
	 * @return
	 */
	public String downloadNewestMessageBoardListStr(int mapId,Date endTime,int pageSize){
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("mapId", String.valueOf(mapId));
		if (endTime != null) {
			map.put("beginTime", sdf.format(endTime));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listCommentNewestUrl, map);
		return ret;
	}
	/**
	 * 求解需要解答的问题的列表
	 * 
	 * @return
	 */
	public String downloadQuesstionMapListStr(String nickName, Date beginTime,
			String myNickName, int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (nickName != null) {
			map.put("nickName", nickName);
		}
		if (beginTime != null) {
			map.put("beginTime", sdf.format(beginTime));
		}
		if (myNickName != null) {
			map.put("myNickName", myNickName);
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.needAnswerMap, map);
		return ret;
	}

	/**
	 * 读取我解决过的地图的列表
	 * 
	 * @param nickName
	 * @param beginTime
	 * @param pageSize
	 * @return
	 */
	public String downloadResolvedMapListStr(String nickName, Date beginTime,
			int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (nickName != null) {
			map.put("nickName", nickName);
		}
		if (beginTime != null) {
			map.put("seperateTime", sdf.format(beginTime));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.myResolvedMapUrl, map);
		return ret;
	}

	/**
	 * 从网络上读取二进制数据的方法
	 * 
	 * @param id
	 * @return
	 */
	public byte[] readMapFromNet(int id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("mapId", String.valueOf(id));
		byte[] ret = netTools.postDataStream(Config.downloadMapFileUrl, map);
		return ret;
	}
	/**
	 * 读取地图的二进制文件如果本地有缓存就读取本地的
	 * @param id
	 * @return
	 */
	public byte[] readMapFromNetOrLocal(int id){
		String filePath = Config.getNetBasePath() + File.separator + String.valueOf(id)+".map";
		File file = new File(filePath);
		//本地文件存在,读取本地文件
		if(file.exists()){
			try {
				return FileUtil.readFileByPath(filePath);
			} catch (IOException e) {
			}
		}
		//本地文件不存在,读取网络文件
		byte [] buf = readMapFromNet(id);
		if(buf != null){
			writeFile(buf,filePath);//写入文件
		}
		return buf;
	}
	// 写入文件
	private void writeFile(byte[] buf, String fileName) {
		FileOutputStream os;
		try {
			os = new FileOutputStream(fileName);
			os.write(buf);
			os.close();
		} catch (Exception e) {
		}
	}
	/**
	 * 从网络上读取二进制答案的方法
	 * @param id
	 * @return
	 */
	public byte [] readAnswerFromNet(int id){
		Map<String,String> map = new HashMap<String,String>();
		map.put("answerId", String.valueOf(id));
		byte [] ret = netTools.postDataStream(Config.downloadAnswer, map);
		return ret;
	}
	/**
	 * 从网络上读取网络详情的方法
	 * @param id
	 * @return
	 */
	public String readMapDetailFromNet(int id){
		Map<String,String> map = new HashMap<String,String>();
		map.put("mapId", String.valueOf(id));
		//myNickName
		SelfMessage self = Config.readSelfMessage(context);
		if(self!=null){
			map.put("myNickName", self.getNickName());
		}
		String ret = netTools.postData(Config.getNetMapDetail, map);
		return ret;
	}
}
